package com.firmboy.tool;

import org.junit.Test;

import java.io.*;

/**
 * 对文件中的字符串进行计数
 *
 * @author firmboy
 * @create 2018-05-05 上午10:02
 **/
public class FileCountTool {

    public static void main(String[] args) throws Exception {
        //count();
        countContain();
    }

    @Test
    public void count() throws IOException {
        String filePath = "/firmboy/workspace/atom/temp/count.txt";
        String keyWord = "199";
        int count = 0;

        File file = new File(filePath);
        FileReader fr = new FileReader(file);
        BufferedReader bfr = new BufferedReader(fr);
        String s = null;
        while((s=bfr.readLine())!=null){
            if(keyWord.equals(s)){
                count++;
            }
        }

        System.out.println("文件中共包含"+keyWord+count+"次");
    }

    public static void countContain() throws IOException {
        String filePath = "/firmboy/workspace/atom/temp/count.txt";
        String keyWord = "归换连接完成";
        int count = 0;

        File file = new File(filePath);
        FileReader fr = new FileReader(file);
        BufferedReader bfr = new BufferedReader(fr);
        String s = null;
        while((s=bfr.readLine())!=null){
            if(s.contains(keyWord)){
                count++;
            }
        }

        System.out.println("文件中共包含"+keyWord+count+"次");
    }

}
